# Build recipe for ncurses.
#
# Copyright (c) 2016-2020 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=ncurses
version=6.1-20200118
pkgversion=$(echo $version | tr - _)
release=2

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/libs"

tarname=${program}-${version}.tgz
termcap_file=termcap-20190609.src.gz

# Remote source(s)
fetch="
  http://invisible-mirror.net/archives/ncurses/current/$tarname
  http://invisible-mirror.net/archives/ncurses/current/$termcap_file
"

description="
Emulation library of Curses.

The Ncurses (new curses) library is a free software emulation of
curses in System V Release 4.0.  The ncurses code was developed
under GNU/Linux.  It has been in use for some time with OpenBSD
as the system curses library, and on FreeBSD and NetBSD as an
external package.

The distribution includes the library and support utilities,
including a terminfo compiler \"tic\", a decompiler \"infocmp\",
clear, tput, tset, and a termcap conversion tool \"captoinfo\".
"

homepage=http://www.gnu.org/software/ncurses
license=Custom

# Source documentation
docs="ANNOUNCE AUTHORS COPYING NEWS README TO-DO VERSION"
docsdir="${docdir}/${program}-${pkgversion}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --enable-widec \
     --enable-symlinks \
     --enable-hard-tabs \
     --enable-xmc-glitch \
     --enable-colorfgbg \
     --enable-termcap \
     --enable-pc-files \
     --disable-rpath-hack \
     --disable-stripping \
     --with-pkg-config-libdir=/usr/lib${libSuffix}/pkgconfig \
     --with-shared \
     --with-gpm \
     --without-tests \
     --without-debug \
     --without-ada \
     --build="$(cc -dumpmachine)"

    make -j${jobs} V=1
    mkdir -p "${destdir}/usr/lib${libSuffix}/pkgconfig"
    make -j${jobs} DESTDIR="$destdir" install

    # Insert termcap file provided by Thomas E. Dickey
    mkdir -p "${destdir}/etc"
    zcat "${tardir}/$termcap_file" > "${destdir}/etc/termcap"
    chmod 644 "${destdir}/etc/termcap"
    unset termcap_file

    touch "${destdir}/etc/.graft-config"

    # Provide non-widec compatibility

    for name in ncurses form panel menu
    do
        rm -f "${destdir}/usr/lib${libSuffix}/lib${name}.so"

        echo "INPUT(-l${name}w)" > \
         "${destdir}/usr/lib${libSuffix}/lib${name}.so"

        echo "INPUT(-l${name}w)" > \
         "${destdir}/usr/lib${libSuffix}/lib${name}.a"

        (
            cd "${destdir}/usr/lib${libSuffix}/pkgconfig" && \
             ln -sf ${name}w.pc ${name}.pc
        )
    done

    major_version=$(echo $version | cut -f 1 -d .)
    for name in tic tinfo
    do
        echo "INPUT(libncursesw.so.${major_version})" > \
         "${destdir}/usr/lib${libSuffix}/lib${name}.so"

        ln -sf libncursesw.so.${major_version} \
         "${destdir}/usr/lib${libSuffix}/lib${name}.so.${major_version}"
    done
    unset major_version

    # Backward compatibility
    (
        cd "${destdir}/usr/lib${libSuffix}" || exit 2
        echo "INPUT(-lncursesw)" > libcursesw.so
        ln -sf libncurses.so libcurses.so
        ln -sf libncurses++w.a libncurses++.a
    )
    # Just in case
    ln -sf ncursesw6-config "${destdir}/usr/bin/ncurses6-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}/"
}

